package com.pudu.mydemo.act;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.pudu.mydemo.view.DashSpinner;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

/**
 * Created by guiliu on 2016/8/24.
 */
public class MapUpLoadActivity extends BaseHomeActivity implements DashSpinner.OnDownloadIntimationListener{
    private DashSpinner mDashSpinner , mDashSpinner1;
    private ImageButton skip;
    private float mnProgress = 0.0f, nProgress = 0.0f;
    private DownLoadRunnable downLoadRunnable;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_map_upload;
    }


    @Override
    public void initView() {
        super.initView();
        mDashSpinner = (DashSpinner) findViewById(R.id.progress_spinner);
        mDashSpinner1 = (DashSpinner)findViewById(R.id.progress_spinner1);
        skip = (ImageButton)findViewById(R.id.skip);
        mDashSpinner.setOnDownloadIntimationListener(this);
        mDashSpinner1.setOnDownloadIntimationListener(this);
        mDashSpinner.resetValues();
        mDashSpinner1.resetValues();
        mnProgress = 0.0f; nProgress = 0.0f;
        //示意图上传
        String mapData = App.getInstance().getApplicationMapData();
        String atlasData = App.getInstance().getApplicationAtlasData();
        if(mapData !=null && mapData.length() > 0 ) {
            mDashSpinner.setVisibility(View.VISIBLE);
        }

        //坐标图上传
        if(atlasData != null && atlasData.length() > 0) {
            mDashSpinner1.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        downLoadRunnable = new DownLoadRunnable();
        new Thread(downLoadRunnable).start();
//        if(!downLoadThread.isAlive())
//        downLoadThread.start();
       /* skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MapUpLoadActivity.this, HomeActivity.class);
                startActivity(intent);
                MapUpLoadActivity.this.finish();
            }
        });*/
        skip.setOnClickListener(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeCallbacksAndMessages(null);
//        downLoadThread.interrupt();
        downLoadRunnable.setMapDown(true);
        downLoadRunnable.setStlasDown(true);
    }

    Handler mHandler = new Handler();
    DownLoadThread downLoadThread = new DownLoadThread();
    //Run to 100% and then show Success
    Runnable runnableSuccess = new Runnable() {
        @Override
        public void run() {
            setProgress();

            //SUCCESS
            if (mnProgress <= 1.0) mHandler.postDelayed(this, 30);
            else mDashSpinner.showSuccess();
        }
    };


    //Run to 100% and then show Success
    Runnable runnableSecondSuccess = new Runnable() {
        @Override
        public void run() {
            setSecondProgress();

            //SUCCESS
            if (nProgress <= 1.0) mHandler.postDelayed(this, 30);
            else mDashSpinner1.showSuccess();
        }
    };



    //Run to 50% and show failure
    Runnable runnableFailure = new Runnable() {
        @Override
        public void run() {
            setProgress();
            //FAILURE
            if (mnProgress <= 0.5) mHandler.postDelayed(this, 30);
            else mDashSpinner.showFailure();
        }
    };

    //Show Unknown Error
    Runnable runnableUnknown = new Runnable() {
        @Override
        public void run() {
            //UNKNOWN
            mDashSpinner.showUnknown();
        }
    };


    @Override
    public void onDownloadIntimationDone(DashSpinner.DASH_MODE dashMode) {
        switch (dashMode) {
            case SUCCESS:
//                Toast.makeText(this, "Download Successful!", Toast.LENGTH_SHORT).show();
                break;
            case FAILURE:
//                Toast.makeText(this, "Download Failed!", Toast.LENGTH_SHORT).show();
                break;
            case UNKNOWN:
//                Toast.makeText(this, "Unknown Download Error!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void setProgress() {
        mnProgress += 0.01;
        mDashSpinner.setProgress(mnProgress);
    }

    private void setSecondProgress() {
        nProgress += 0.01;
        mDashSpinner1.setProgress(nProgress);
    }

    class DownLoadRunnable implements Runnable {
        private volatile boolean mapDown, stlasDown;
        public void setMapDown(boolean mapDown) {
            this.mapDown = mapDown;
        }

        public void setStlasDown(boolean stlasDown) {
            this.mapDown = stlasDown;
        }
        @Override
        public void run() {
            while (!mapDown || !stlasDown) {
//               Log.e("DownLoadThread","mapDown:"+mapDown+"　stlasDown:"+stlasDown);
                if(App.getInstance().getApplicationMapData() != null && App.getInstance().getApplicationMapData().length() > 0 && !mapDown) {
                    mapDown = true;
                    Log.e("DownLoadThread","示意图已经上传");
                    mHandler.post(runnableSuccess);
                }


                if(App.getInstance().getApplicationAtlasData() != null && App.getInstance().getApplicationAtlasData().length() > 0 && !stlasDown) {
                    stlasDown = true;
                    Log.e("DownLoadThread","坐标图已经上传");
                    mHandler.post(runnableSecondSuccess);
                }
            }
        }
    }


   class DownLoadThread extends Thread{
       private volatile boolean mapDown, stlasDown;
       public void setMapDown(boolean mapDown) {
           this.mapDown = mapDown;
       }

       public void setStlasDown(boolean stlasDown) {
           this.mapDown = stlasDown;
       }
       @Override
       public void run() {
           super.run();

           while (!mapDown || !stlasDown) {
//               Log.e("DownLoadThread","mapDown:"+mapDown+"　stlasDown:"+stlasDown);
               if(App.getInstance().getApplicationMapData() != null && App.getInstance().getApplicationMapData().length() > 0 && !mapDown) {
                   mapDown = true;
                   Log.e("DownLoadThread","示意图已经上传");
                   mHandler.post(runnableSuccess);
               }


               if(App.getInstance().getApplicationAtlasData() != null && App.getInstance().getApplicationAtlasData().length() > 0 && !stlasDown) {
                    stlasDown = true;
                   Log.e("DownLoadThread","坐标图已经上传");
                    mHandler.post(runnableSecondSuccess);
               }
           }

       }
   }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v == skip) {
            Intent intent = new Intent(MapUpLoadActivity.this, HomeActivity.class);
            startActivity(intent);
            MapUpLoadActivity.this.finish();
        }
    }

    @Override
    public String getClickAction(View v) {
        if(v == skip) {
            return "跳过";
        }
        return super.getClickAction(v);
    }
}
